package com.example.demo.controller;

import com.example.demo.model.BookInfo;
import com.example.demo.model.PageRequest;
import com.example.demo.model.PageResult;
import com.example.demo.service.BookService;
import jakarta.persistence.criteria.CriteriaBuilder;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.awt.print.Book;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/book")
public class BookController {
    @Autowired
    private BookService bookService;

    @RequestMapping("/getPage")
    public PageResult getPage(PageRequest pageRequest) {
        log.info("请求 pageNum " + pageRequest.getPageNum() + " offset " + pageRequest.getOffset() + " pageSize " + pageRequest.getPageSize());
        return bookService.getPageResult(pageRequest);
    }

    @RequestMapping("/add")
    public Boolean addBook(BookInfo bookInfo) {
        if(!StringUtils.hasLength(bookInfo.getBookName()) ||
            !StringUtils.hasLength(bookInfo.getAuthor()) ||
            !StringUtils.hasLength(bookInfo.getPublish()) ||
            bookInfo.getCount() == null ||
            bookInfo.getStatus() == null ||
            bookInfo.getPrice() == null) {
            return false;
        }
        Boolean ret = bookService.addBook(bookInfo);
        return ret;
    }

    @RequestMapping("/getSingle")
    public BookInfo getSingle(Integer bookId) {
        return bookService.getBookById(bookId);
    }

    @RequestMapping("/update")
    public Boolean update(BookInfo bookInfo) {
        return bookService.update(bookInfo);
    }

    @RequestMapping("/deleteSingle")
    public Boolean deleteSingle(Integer id) {
        return bookService.deleteById(id);
    }

    @RequestMapping("/deleteBatch")
    public Boolean deleteBatch(@RequestParam List<Integer> ids) {
        return bookService.deleteByBatch(ids);
    }
}
